//
//  BaseCollectionView.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/12.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class BaseCollectionView: UICollectionView {

    
    internal var flowLayout: BaseFlowLayout?
    
    internal lazy var ts_dataArray: Array<AnyObject>? = [AnyObject]()
    
    required override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        
        flowLayout = layout as? BaseFlowLayout
        
        commitInitOwnProperties()
        
        configOwnSubviews()
        
        addOwnSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}


extension BaseCollectionView {
    
    func commitInitOwnProperties() {
        
        showsVerticalScrollIndicator = false
        
        showsHorizontalScrollIndicator = false
        
        bounces = false
        
        dataSource = self
        
        delegate = self
        
        keyboardDismissMode = .OnDrag
        
    }
}


extension BaseCollectionView {
    
    internal override class func getInstance() -> Self {
        
        let layout = BaseFlowLayout()
        
        return self.init(frame: CGRectZero, collectionViewLayout: layout)
    }
    internal class func getInstance(frame: CGRect) -> Self {
        let layout = BaseFlowLayout()
        
        return self.init(frame: frame, collectionViewLayout: layout)
    }
    internal class func getInstance(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) -> Self {
        
        return self.init(frame: frame, collectionViewLayout: layout)
    }

    
}

extension BaseCollectionView: UICollectionViewDataSource {
    
    internal func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    internal func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ts_dataArray!.count
    }
    internal func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell: UICollectionViewCell = UICollectionViewCell()
        
        return cell
    }
}

extension BaseCollectionView: UICollectionViewDelegate {
    
    internal func defaultSelected(index: Int) {
        
        selectItemAtIndexPath(NSIndexPath(forRow: index,inSection: 0), animated: true, scrollPosition: .None)
        
    }
    
    internal func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
        
        mDelegate?.onItemClickType!("CollectionViewItem", data: ts_dataArray![indexPath.row], indexPath: indexPath)
    }
}

extension BaseCollectionView: UICollectionViewDelegateFlowLayout {
    
    internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSizeZero
    }
    internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSizeZero
    }
    internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        
        return CGSizeZero
    }
    internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }
    internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }
    internal func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }
}

extension BaseCollectionView: CMDBBaseViewDelegate {
    override func onItemClickType(itemClickType: String?, data: AnyObject?, indexPath: NSIndexPath?) {
        mDelegate?.onItemClickType!(itemClickType, data: data, indexPath: indexPath)
    }
}
